home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0493 / FASTMATH.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-22  |  1KB  |  36 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 430 of 473
  3. From : Erik Johnson                        1:104/28.0           13 Apr 93  19:52
  4. To   : David Jirku
  5. Subj : FAST MATH
  6. ────────────────────────────────────────────────────────────────────────────────
  7. >        I was just wondering how to speed up some math-intensive
  8. >        routines I've got here. For example, I've got a function
  9. >        that returns the distance between two objects:
  10. >
  11. >        Function Dist(X1,Y1,X2,Y2 : Integer) : Real;
  12. >
  13. >        BEGIN
  14. >          Dist := Round(Sqrt(Sqr(X1-X2)+Sqr(Y1-Y2)));
  15. >        END;
  16. >
  17. >        This is way to slow. I know assembly can speed it up, but
  18. >        I know nothing about asm. so theres the problem. Please
  19. >        help me out, any and all source/suggestions welcome!
  20.  
  21. X1, Y1, X2, Y2 are all integers.  Integer math is faster than Real (just
  22. about anything is).  Sqr and Sqrt are not Integer functions.  Try for
  23. fun...     }
  24.  
  25. Function Dist( X1, Y1, X2, Y2 : Integer ) : Real;
  26. VAR XTemp, YTemp : INTEGER; {the allocation of these takes time.  If you
  27.                              don't want that time taken, make them
  28.                              global with care}
  29. BEGIN
  30.   XTemp := X1 - X2;
  31.   YTemp := Y1 - Y2;
  32.   Dist := Round( Sqrt( XTemp*XTemp + YTemp*YTemp ));
  33. END;
  34.  
  35. If you have a math coprocessor or a 486dx, try using DOUBLE instead of
  36. REAL, and make sure your compiler is set to compile for 287 (or 387).